package http

import (
	"github.com/gin-gonic/gin"
)

// Response 默认响应格式
type Response struct {
	Code  int    `json:"code"`
	Data  any    `json:"data"`
	Token string `json:"token,omitempty"`
	Msg   string `json:"msg"`
	Ok    bool   `json:"ok"`
}

// ResponseHandle 响应工具
func ResponseHandle(c *gin.Context, response Response, statusCode ...int) {
	if token, exists := c.Get("token"); exists {
		if token, ok := token.(string); ok {
			response.Token = string(token)
		}
	}
	code := 200
	if len(statusCode) > 0 && statusCode[0] > 0 {
		code = statusCode[0]
	}
	c.JSON(code, response)
}
